<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            margin: 0;
            padding: 0;
        }
        a{
            text-decoration: none;
        }
        table,th,td{
            padding: 10px 5px;
            border: 1px solid black;
            border-spacing: 0;
            border-collapse: collapse;
        }
        table{
            width: 400px;
            margin-top: 30px;
            text-align: center;
        }
        tr td span{
            color: #701A8B;
        }
        tr td span:hover{
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1>用户列表</h1>
    <a href="./add.html">添加用户</a>
    <table>
        <tr>
            <th>id</th>
            <th>用户名</th>
            <th>密码</th>
            <th>操作</th>
        </tr>
    </table>
    <script src="./node_modules/axios/dist/axios.js"></script>
    <script>
        const url = "http://localhost:3000/users";
        const table = document.querySelector("table");
        axios.get(url).then(res=>{
            const data = res.data;
            //遍历
            table.innerHTML += data.map(item=>{
                return `<tr>
                            <td>${item.id}</td>
                            <td>${item.username}</td>
                            <td>${item.password}</td>
                            <td>
                                <a href="./edit.html?id=${item.id}">编辑</a>
                                <span onclick=toDelete(${item.id})>删除</span>
                            </td>
                        </tr>`
            }).join("");


        });

        //删除的

        const toDelete = (id)=>{
            const res = confirm("你确认到删除吗？");
            if(res){
                axios({
                    method:"delete",
                    url:`http://localhost:3000/users/${id}`,
                }).then(()=>{
                    location.href = "./index.html";
                })
            }
        }

    </script>
</body>
</html>